setwd("/Users/bsjjones/Dropbox/JoestenJones/PB R&R")

#This file creates tables-as-figures plots for the micro models as plots empahsis by position.  The Stata 
#do file that generates the input data is "replicationfile1_districtmodels.do"  


library(foreign)
library(rmeta)
library(plotrix)
pdf("figureS3_districtmodels.pdf", width=7, height=7)
par(mfrow=c(2,3) ,oma=c(0,2,4,0), mar=c(3,4,3,1))


#CONDITIONAL MODEL#
priority<-read.dta("districtmodel.dta")
attach(priority)
coef.vec<-beta
se.vec<-sebeta
y.axis<-c(length(coef.vec):1)
var.names<-label

#set margins for plot, leaving lots of room on left-margin (2nd number in margin command) for variable names

plot(coef.vec, y.axis, type = "p", axes = F, xlab = "Regression Coefficient", ylab = "", pch = 20, cex = .85, xlim = c(-2.25,1.25), xaxs = "r", main = "A. Candidate Priority", cex.main=.85, cex.lab=.65, mgp=c(2,.7,0)) 
#plot coefficients as points, turning off axes and labels. 

#set limits of x-axis so that they include mins and maxs of 
#coefficients + .95% confidence intervals and plot is symmetric; use "internal axes", and leave plot title empty
#the 3 lines below create horiztonal lines for 95% confidence intervals, and vertical ticks for 90% intervals

segments(coef.vec-qnorm(.975)*se.vec, y.axis, coef.vec+qnorm(.975)*se.vec, y.axis, lwd =  1, lend="butt")

#coef +/-1.96*se = 95% interval, lwd adjusts line thickness
#if you want to add tick marks for 90% confidence interval, use following 2 lines:
#segments(coef.vec-qnorm(.95)*se.vec, y.axis -.1, coef.vec-qnorm(.95)*se.vec, y.axis +.1, lwd = 1.1)#coef +/-1.64*se = 90% interval
#segments(coef.vec+qnorm(.95)*se.vec, y.axis -.1, coef.vec+qnorm(.95)*se.vec, y.axis +.1, lwd = 1.1)

axis(1, at = seq(-2.5,1.25,by=.5), labels = NA, tick = T,#draw x-axis and labels with tick marks
    cex.axis = .65, mgp = c(2,.7,0))#reduce label size, moves labels closer to tick marks
axis(1, at = seq(-2.5,1.5,by=.5), labels =  c(-2.5, -2, -1.5, -1, -.5, 0, .5, 1, 1.5 ), tick = T,#draw x-axis and labels with tick marks
    cex.axis = .65, mgp = c(2,.7,0))#reduce label size, moves labels closer to tick marks    
 axis(2, at = y.axis, label = var.names,   las = 1, tick = T, ,mgp = c(2,.6,0),
    cex.axis = .65) #draw y-axis with tick marks, make labels perpendicular to axis and closer to axis
segments(0,0,0,14,lty=2) # draw dotted line through 0
box(bty = "l") #place box around plot
x.height <- .20
text(x.height, 2.98, expression(R^{2} == .53), adj = 0, cex = .5) #add text for R-squared
text(x.height, 2.55, expression(beta[0] == 2.93~(.18)), adj = 0, cex = .5)#add text for Adjusted-R-squared
text(x.height, 2.22, "n = 300", adj = 0, cex = .5)#add text for sample size

rm(list=ls())
detach(priority)






##################
#Plots of model quantities: First is Position and Priority for Dems and Republicans#
##################


rpri<-read.dta("posplot_GOP.dta")
attach(rpri)
coef.vec<-pr1
se.vec<-se
x.axis<-c(length(coef.vec):1)
#var.names<-label
emph<-seq(4, 7, .5)

dpri<-read.dta("posplot_DEM.dta")
attach(dpri)
coef.vec2<-pr1
se.vec2<-se
x.axis2<-c(length(coef.vec):1)
#var.names<-label
emph2<-seq(1, 4, .5)

plotCI(emph, coef.vec, 1.96*se.vec,  1.96*se.vec, lwd = 1, col = "black", scol = "black", lty=1, axes = TRUE, xlab = "Candidate Position", ylab = "Predicted Priority", pch = 20, cex.axis = .65, main = "B. Priority and Position", cex.main=.85, cex.lab=.65, mgp=c(2,.7,0), ylim=c(1,4), xlim=c(1,7))
lines(emph, coef.vec, type="l", col="black")

 
par(new=TRUE)

plotCI(emph2, coef.vec2, 1.96*se.vec2,  1.96*se.vec2, lwd = 1, col = "gray50", scol = "gray50", ylim=c(1,4), ylab="", xlab="", pch= 20, axes=F, xlim=c(1,7))
lines(emph2, coef.vec2, type="l", col="gray50")

legend("topleft", c("Republican", "Democrat"), lty=c(1,1), lwd=c(1,1), col=c("black", "gray50"), bty="n", cex=.65)

rm(list=ls())
detach(rpri)
detach(dpri)




##############
#New Destination and Immigration by Party DEMOCRATS
##############


rpri1<-read.dta("immDNDplot_DEM.dta")
attach(rpri1)
coef.vec<-pr1
se.vec<-se
x.axis<-c(length(coef.vec):1)
#var.names<-label
emph<-seq(0, 1, .1)
rpri2<-read.dta("immDnNDplot_DEM.dta")
attach(rpri2)
coef.vec2<-pr1
se.vec2<-se
x.axis2<-c(length(coef.vec):1)
#var.names<-label
emph2<-seq(0, 1, .1)

plotCI(emph, coef.vec, 1.96*se.vec,  1.96*se.vec, lwd = 1, col = "gray50", scol = "gray50", lty=1, axes = TRUE, xlab = "Immigration Opinion", ylab = "Predicted Priority", pch = 20, cex.axis = .65, main = "C. Immigration Opinion and \n New Destination States: Democrats", cex.main=.85, cex.lab=.65, mgp=c(2,.7,0), ylim=c(1,4), xlim=c(0,1))
lines(emph, coef.vec, type="l", col="gray50")

 
par(new=TRUE)

plotCI(emph2, coef.vec2, 1.96*se.vec2,  1.96*se.vec2, lwd = 1, col = "gray50", scol = "gray50", ylim=c(1,4), ylab="", xlab="", pch= 20, axes=F, xlim=c(0,1))
lines(emph2, coef.vec2, type="l", col="gray50", lty=3)

legend("topleft", c("Democrat, New Destination", "Democrat, Non-New Destination"), lty=c(1,3), lwd=c(1,1), col=c( "gray50" , "gray50"), bty="n", cex=.65)
title(main="Prioritization of Illegal Immigration Issue as a function of \n Candidate, District, and Population Characteristics", outer=TRUE)

rm(list=ls())
detach(rpri2)
detach(dpri2)



##############
#New Destination and Immigration by Party REPUBLICANS
##############


rpri1<-read.dta("immRNDplot_GOP.dta")
attach(rpri1)
coef.vec<-pr1
se.vec<-se
x.axis<-c(length(coef.vec):1)
#var.names<-label
emph<-seq(0, 1, .1)

rpri2<-read.dta("immRnNDplot_GOP.dta")
attach(rpri2)
coef.vec2<-pr1
se.vec2<-se
x.axis2<-c(length(coef.vec):1)
#var.names<-label
emph2<-seq(0, 1, .1)

plotCI(emph, coef.vec, 1.96*se.vec,  1.96*se.vec, lwd = 1, col = "black", scol = "black", lty=1, axes = TRUE, xlab = "Immigration Opinion", ylab = "Predicted Priority", pch = 20, cex.axis = .65, main = "D. Immigration Opinion and \n New Destination States: Republicans", cex.main=.85, cex.lab=.65, mgp=c(2,.7,0), ylim=c(1,4), xlim=c(0,1))
lines(emph, coef.vec, type="l", col="black")

par(new=TRUE)

plotCI(emph2, coef.vec2, 1.96*se.vec2,  1.96*se.vec2, lwd = 1, col = "black", scol = "black", ylim=c(1,4), ylab="", xlab="", pch= 20, axes=F, xlim=c(0,1))
lines(emph2, coef.vec2, type="l", col="black", lty=3)

legend("topleft", c("Republican, New Destination", "Republican, Non-New Destination"), lty=c(1,3), lwd=c(1,1), col=c("black", "black"), bty="n", cex=.65)

rm(list=ls())
detach(rpri2)
detach(dpri2)





##################
#Plots of model quantities: District Hispanic#
##################


rpri<-read.dta("hispplot_GOP.dta")
attach(rpri)
coef.vec<-pr1
se.vec<-se
x.axis<-c(length(coef.vec):1)
#var.names<-label
emph<-seq(0, .8, .1)

dpri<-read.dta("hispplot_DEM.dta")
attach(dpri)
coef.vec2<-pr1
se.vec2<-se
x.axis2<-c(length(coef.vec):1)
#var.names<-label
emph2<-seq(0, .8, .1)

plotCI(emph, coef.vec, 1.96*se.vec,  1.96*se.vec, lwd = 1, col = "black", scol = "black", lty=1, axes = TRUE, xlab = "District Hispanic", ylab = "Predicted Priority", pch = 20, cex.axis = .65, main = "E. District Hispanic Population \n and Issue Priority", cex.main=.85, cex.lab=.65, mgp=c(2,.7,0), ylim=c(1,4), xlim=c(0,.8))
lines(emph, coef.vec, type="l", col="black")

 
par(new=TRUE)

plotCI(emph2, coef.vec2, 1.96*se.vec2,  1.96*se.vec2, lwd = 1, col = "gray50", scol = "gray50", ylim=c(1,4), ylab="", xlab="", pch= 20, axes=F, xlim=c(0,.8))
lines(emph2, coef.vec2, type="l", col="gray50")

legend("topleft", c("Republican", "Democrat"), lty=c(1,1), lwd=c(1,1), col=c("black", "gray50"), bty="n", cex=.65)

rm(list=ls())
detach(rpri)
detach(dpri)
dev.off()



